package com.example.view;
import org.json.JSONObject;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.app.MyApplication;
import com.example.tool.BaseActivity;
import com.example.tool.LanguageAdapter;
import com.example.tool.Utils;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class NetSettingActivity extends BaseActivity {
	private ImageView title_left_btn;
	private EditText et_ip, et_port;
	private TextView title_tv;
	private TextView tv_freshen, tv_right_btn, tv_freshen2;
	private String ip = "";
	private String port = "";
	RequestQueue mQueue =null;
	private String[] data = { "中文", "English", "Tiếng Việt" };
	JsonObjectRequest mJsonObjectRequest=null;
	private Spinner sp;
	private TextView tv_port;
	private TextView tv_status;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_net_setting);
		MyApplication.mylist.add(this);
		ip = MyApplication.getServersInfo().getString("serverip", "192.168.111.110");
		port = MyApplication.getServersInfo().getString("serverport", "8090");
		mQueue=MyApplication.getRequestQueue();
		initView();
	}

	private void initView() {
		title_left_btn = (ImageView) findViewById(R.id.title_left_btn);
		title_left_btn.setVisibility(View.VISIBLE);
		title_tv = (TextView) findViewById(R.id.title_tv);
		title_tv.setText(R.string.networksetting);
		tv_right_btn = (TextView) findViewById(R.id.tv_right_btn);
		tv_right_btn.setText(R.string.save);
		tv_freshen = (TextView) findViewById(R.id.tv_freshen);
		tv_freshen.setText(R.string.refresh);
		tv_freshen2 = (TextView) findViewById(R.id.tv_freshen2);
		tv_freshen2.setVisibility(View.GONE);
		tv_port=(TextView)findViewById(R.id.tv_port);
		tv_port.setText(R.string.port);
		tv_status=(TextView)findViewById(R.id.tv_status);
		tv_status.setText(R.string.status);
		et_ip = (EditText) findViewById(R.id.tv_ip_num);
		et_port = (EditText) findViewById(R.id.et_port);
		title_left_btn.setFocusable(true);
		title_left_btn.setFocusableInTouchMode(true);
		tv_right_btn.setFocusable(true);
		tv_right_btn.setFocusableInTouchMode(true);
		tv_freshen.setFocusable(true);
		tv_freshen.setFocusableInTouchMode(true);
		et_ip.setText(ip);
		et_port.setText(port);
		et_ip.setFocusable(true);
		et_ip.setFocusableInTouchMode(true);
		et_port.setFocusable(true);
		et_port.setFocusableInTouchMode(true);
		title_left_btn.setOnClickListener(this);
		tv_freshen.setOnClickListener(this);
		tv_right_btn.setOnClickListener(this);

		sp = (Spinner) findViewById(R.id.tv_sp);
		LanguageAdapter adapter = new LanguageAdapter(this, data);
		sp.setAdapter(adapter);
		if(MyApplication.yy.equals("zh"))
			sp.setSelection(0);
		else if(MyApplication.yy.equals("en"))
			sp.setSelection(1);
		else
			sp.setSelection(2);
		sp.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				switch (position) {
					case 0:
						updateActivity("zh",NetSettingActivity.class);
						MyApplication.yy = "zh";
						MyApplication.getSharedUserInfo().edit().putString("language", "zh").apply();
						break;
					case 1:
						updateActivity("en",NetSettingActivity.class);
						MyApplication.yy = "en";
						MyApplication.getSharedUserInfo().edit().putString("language", "en").apply();
						break;
					case 2:
						updateActivity("vi",NetSettingActivity.class);
						MyApplication.yy = "vi";
						MyApplication.getSharedUserInfo().edit().putString("language", "vi").apply();
						break;
					default:
						break;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}
		});
	}

	@Override
	protected void onDestroy() {
		MyApplication.mylist.remove(this);
		super.onDestroy();
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.title_left_btn:
				finish();
				break;
			case R.id.tv_right_btn:
				ip = et_ip.getText().toString();
				port = et_port.getText().toString();
				if (ip.equals("")) {
					showToast(R.string.input_ip);
					return;
				}
				if (port.equals("")) {
					showToast(R.string.inputport);
					return;
				}

				MyApplication.getServersInfo().edit().putString("serverip", ip).apply();
				MyApplication.getServersInfo().edit().putString("serverport", port).apply();
				MyApplication.SERVERPATH = "http://" + ip + ":" + port;
				showToast(R.string.save_ok);
				finish();
				break;
			case R.id.tv_freshen:
				ip = et_ip.getText().toString();
				port = et_port.getText().toString();
				if (ip.equals("")) {
					showToast(R.string.input_ip);
					return;
				}
				if (port.equals("")) {
					showToast(R.string.inputport);
					return;
				}
				boolean isConnected = Utils.toastIsNetworkConnected(context);
				if (isConnected) {
					showProgressBar();
					String url="http://" + ip + ":" + port+ "/Y502/getVarsion";
					//RequestQueue mQueue = Volley.newRequestQueue(getApplicationContext());
					mJsonObjectRequest = new JsonObjectRequest(Request.Method.POST,url,
							null, new Response.Listener<JSONObject>() {
						@Override
						public void onResponse(JSONObject response) {
							hideProgressBar();
							tv_freshen2.setVisibility(View.VISIBLE);
							tv_freshen2.setText(R.string.success);
							tv_freshen2.setTextColor(getResources().getColor(R.color.color_blue_normal));
						}
					}, new Response.ErrorListener() {
						@Override
						public void onErrorResponse(VolleyError error) {
							hideProgressBar();
							tv_freshen2.setVisibility(View.VISIBLE);
							tv_freshen2.setText(R.string.fail);
							tv_freshen2.setTextColor(getResources().getColor(R.color.color_red_normal));
						}
					}
					);
					mQueue.add(mJsonObjectRequest);
				}
				else
				{
					showToast(getString(R.string.network_error_try_again));
					return;
				}
				break;
			default:
				break;
		}
	}
}
